<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\index\controller;

use think\admin\Controller;


/**
 * Class Edu
 * @package app\index\controller
 */
class Edu extends BaseController
{

    //培训教育所对应的分类ID
    static private $category_id = 12;
    public $table = "CmsEduEnroll";
    public $PASS_STATE_OPTION = array(0=>"",1=>"合格",2=>"不合格");
    public $CITY_OPTION = array("长沙","株洲","湘潭","益阳","衡阳","常德","岳阳","邵阳","娄底","永州","郴州","怀化","张家界","湘西自治州");
    static public $limit = 6;

    //培训教育的二级主页
    public function index()
    {
        $this->title = '培训教育';

        $this->_get_nav_menus();
        $this->_system();

        $this->category = $this->_get_sub_category(self::$category_id);
        for($i=0; $i<count($this->category); $i++){
            $this->category[$i]['articles'] = $this->_get_category_articles($this->category[$i]['id']);
        }

        $this->fetch();
    }

    //列表页
    public function list()
    {


        $this->id = $this->request->get("id");
        $category = $this->_get_sub_category(self::$category_id);
        $selected = $this->id?$this->id:$category[0]['id'];
        $this->kw = $this->intro = $this->request->get("kw");
        $this->page = intval($this->request->get("page",1));
        $pager = $this->_prepare_page_info($selected,self::$limit,$this->kw);
        $offset = ($this->page-1)*self::$limit;

        $map = array("is_deleted"=>"0", "category_id"=>$selected);
        $query = $this->app->db->name("CmsArticles")->where($map)
            ->whereLike("title|content","%".$this->kw."%");
        $list = $query->order("id desc")->limit($offset,self::$limit)->select()->toArray();
        $this->edu = array(
            "info" => $this->_get_category(self::$category_id), //栏目
            "category" => $category,//获取栏目下所有分类
            "selected" => $this->_get_category($selected),//获取栏目单个子栏目
            "list" => $list,
            "pager" => $pager,
        );
        $this->title = $this->edu['selected']['title'];

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();

    }

    //详情页
    public function detail()
    {

        $id = $this->request->get("id");
        $this->article = $this->_get_article($id);
        $this->category = $this->_get_category($this->article['category_id']);
        $this->_get_nav_menus();
        $this->_system();
        if($this->category['is_private']==1 && session('member')==null){
            $this->fetch("common/accessDenied");
        }else{
            $this->_inc_read_num($id);
            $this->title = $this->article['title'];
            $this->fetch("common/detail");
        }
    }

    //我的证书
    public function cert(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $this->classes = $this->_get_class_enroll();
        for($i=0; $i<count($this->classes); $i++){
            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
        }
        $this->classesJson = json_encode($this->classes);

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    //培训报名
    public function enroll(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $this->classes = $this->_get_class_enroll();
        //var_dump($this->app->db->name('CmsEduClass')->getLastSql());
        for($i=0; $i<count($this->classes); $i++){
            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
        }
        $this->classesJson = json_encode($this->classes);

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    //我的报名
    public function classlist(){


        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $this->classes = $this->_get_class_enroll();

        for($i=0; $i<count($this->classes); $i++){

            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);

        }
        $this->classesJson = json_encode($this->classes);

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    public function classinfo(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $this->classes = $this->_get_class_enroll();
        for($i=0; $i<count($this->classes); $i++){
            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
        }
        $this->classesJson = json_encode($this->classes);

        $map = [ "id"=>$this->request->get("id") ];
        $this->info = $this->app->db->name('CmsEduEnroll')->where($map)->find();
        $map = [ "id"=>$this->info['class_id'] ];
        $this->class = $this->app->db->name('CmsEduClass')->where($map)->find();
        $this->fetch();
    }


    public function classdel(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $map = [ 'id'=>$this->request->post("id") ];
        $enroll = $this->app->db->name('CmsEduEnroll')->where($map)->find();
        $data = array("idno"=>$enroll['idno']."_del");
        $this->app->db->name('CmsEduEnroll')->where($map)->update($data);
        $this->_delete("CmsEduEnroll");
    }

    public function classedit(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        if($this->request->isGet()){

            //班级与工作岗位
            $map = [ "id"=>$this->request->get("id") ];
            $this->info = $this->app->db->name('CmsEduEnroll')->where($map)->find();

            $this->classes = $this->_get_class_enroll();
            //var_dump($this->app->db->name('CmsEduClass')->getLastSql());
            for($i=0; $i<count($this->classes); $i++){
                $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
            }
            $this->classesJson = json_encode($this->classes);

            $map = [ "id"=>$this->info["class_id"] ];
            $this->class = $this->app->db->name('CmsEduClass')->where($map)->find();
            $this->position = $this->_get_work_ids($this->class['position_ids']);

            $this->fetch();
        }else{
            $data = $this->request->post();

            $map = [ "ew.id"=>$data['position_id'] ];
            $work = $this->app->db->name('CmsEduWork')->alias("wp")
                ->field("wp.id as work_id, wp.name as work_name,ew.name as position_name, ew.id as position_id")
                ->join('cms_edu_work ew', "wp.id=ew.parent_id")
                ->where($map)
                ->order('wp.id desc')->find();
            $data['work_id'] = $work['work_id'];
            $data['work_name'] = $work['work_name'];
            $data['position_name'] = $work['position_name'];

            $map = [ "id"=> $data['id'] ];
            $this->info = $this->app->db->name('CmsEduEnroll')->where($map)->update($data);
            $this->success("操作成功");
        }
    }

    //考核报名
    public function examenroll(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $this->classes = $this->_get_exam_enroll();
        for($i=0; $i<count($this->classes); $i++){
            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
        }
        $this->classesJson = json_encode($this->classes);

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    //我的考核
    public function examlist(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $this->classes = $this->_get_exam_enroll();
        for($i=0; $i<count($this->classes); $i++){
            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
        }
        $this->classesJson = json_encode($this->classes);

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    public function examinfo(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $this->classes = $this->_get_exam_enroll();
        for($i=0; $i<count($this->classes); $i++){
            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
        }
        $this->classesJson = json_encode($this->classes);

        $map = [ "id"=>$this->request->get("id") ];
        $this->info = $this->app->db->name('CmsEduExamEnroll')->where($map)->find();
        $map = [ "id"=>$this->info['class_id'] ];
        $this->class = $this->app->db->name('CmsEduExam')->where($map)->find();
        $this->fetch();
    }

    public function examdel(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        $map = [ 'is_deleted'=>0,'id'=>$this->request->post("id") ];
        $enroll = $this->app->db->name('CmsEduExamEnroll')->where($map)->find();
        $data = array("idno"=>$enroll['idno']."_del");
        $this->app->db->name('CmsEduExamEnroll')->where($map)->update($data);
        $this->_delete("CmsEduExamEnroll");
    }

    public function examedit(){

        if(session("member")==null){
            $this->redirect(sysuri('/login'));
        }

        if($this->request->isGet()){
            $this->classes = $this->_get_exam_enroll();
            for($i=0; $i<count($this->classes); $i++){
                $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
            }
            $this->classesJson = json_encode($this->classes);

            //班级与工作岗位
            $map = [ "id"=>$this->request->get("id") ];
            $this->info = $this->app->db->name('CmsEduExamEnroll')->where($map)->find();
            $map = [ "id"=>$this->info["exam_id"] ];
            $this->class = $this->app->db->name('CmsEduExam')->where($map)->find();
            $this->position = $this->_get_work_ids($this->class['position_ids']);

            $this->fetch();
        }else{
            $data = $this->request->post();

            $map = [ "ew.id"=>$data['position_id'] ];
            $work = $this->app->db->name('CmsEduWork')->alias("wp")
                ->field("wp.id as work_id, wp.name as work_name,ew.name as position_name, ew.id as position_id")
                ->join('cms_edu_work ew', "wp.id=ew.parent_id")
                ->where($map)
                ->order('wp.id desc')->find();
            $data['work_id'] = $work['work_id'];
            $data['work_name'] = $work['work_name'];
            $data['position_name'] = $work['position_name'];

            $map = [ "id"=> $data['id'] ];
            $this->info = $this->app->db->name('CmsEduExamEnroll')->where($map)->update($data);
            $this->success("操作成功");
        }
    }

    public function enroll2(){

        $this->classes = $this->_get_class_enroll();
        for($i=0; $i<count($this->classes); $i++){
            $this->classes[$i]['sub'] = $this->_get_work_ids($this->classes[$i]['position_ids']);
        }
        $this->classesJson = json_encode($this->classes);

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    public function _get_work_ids($position_ids){
        $work_ids = [];
        $position = $this->app->db->name('CmsEduWork')->alias("wp")
            ->field("wp.*,ew.name as class_name, ew.id as class_id")
            ->join('cms_edu_work ew', "ew.id=wp.parent_id")
            ->whereIn("wp.id", $position_ids)
            ->order('wp.id desc')->select()->toArray();

        for($i=0; $i<count($position); $i++){
            if(array_key_exists($position[$i]['class_id'], $work_ids)==false){
                $work_ids[ $position[$i]['class_id'] ] = array(
                    "id" => $position[$i]['class_id'],
                    "name" => $position[$i]['class_name'],
                    "sub" => [],
                );
            }
        }

        for($i=0; $i<count($position); $i++){
            $work_ids[ $position[$i]['class_id'] ]['sub'][] = $position[$i];

        }

        return array_values($work_ids);
    }



    public function _getEduPosition(){
        $map = [ "parent_id"=>0 ];
        $course = $this->app->db->name('CmsEduWork')->where($map)
            ->order('id asc')->select()->toArray();
        for($i=0; $i<count($course); $i++){
            $map = ["parent_id"=>$course[$i]['id']];
            $course[$i]['sub'] = $this->app->db->name('CmsEduWork')->where($map)
                ->order('id desc')->select()->toArray();
        }
        return $course;
    }

    //培训结果查询
    public function query(){

        $this->classes = $this->_get_class_enroll();

        $this->_get_nav_menus();
        $this->_system();
        $this->fetch();
    }

    public function queryresult(){

        if($this->request->isPost()){
            $data = $this->request->post();
            $this->result = $this->_query_score($data);
            $this->_get_nav_menus();
            $this->_system();
            $this->fetch();

        }else{
            //echo "不支持的请求方式";
            $this->result = array("status"=>1,"score"=>"99分","is_pass"=>"1");
            //$this->result = array("status"=>0,"msg"=>"注册失败");
            $this->_get_nav_menus();
            $this->_system();
            $this->fetch();
        }
    }

    public function _query_score($data){
        $map = array("exam_no"=>$data['exam_no']);
        $enroll = $this->app->db->name("CmsEduExamEnroll")->where($map)->find();
        if($enroll==null){
            return array("status"=>0,"msg"=>"无效的准考证");
        }

        $map = array("exam_no"=>$data['exam_no'],"idno"=>$data['idno']);
        $enroll = $this->app->db->name("CmsEduExamEnroll")->where($map)->find();
        if($enroll==null){
            return array("status"=>0,"msg"=>"准考证与身份证信息不匹配");
        }

        if($enroll['is_pass']==0){
            return array("status"=>0, "msg"=>"成绩尚未公示");
        }

        return array("status"=>1, "score"=>$enroll['score'],"is_pass"=>$enroll['is_pass']);
    }

    public function _generate_enroll_no($student){
        $data = array(
            "idno" => $student['idno'],
            "name" => $student['name'],
            "phone" => $student['phone'],
        );
        return $this->app->db->name("CmsEduEnrollNo")->insertGetId($data);
    }

    public function _generate_exam_enroll_no($student){
        $data = array(
            "idno" => $student['idno'],
            "name" => $student['name'],
            "phone" => $student['phone'],
        );
        return $this->app->db->name("CmsEduExamEnrollNo")->insertGetId($data);
    }

    //查询12个月内所有正在接受报名的开班信息
    public function _get_class_enroll(){
        $map = array("status"=>1);
        return $this->app->db->name('CmsEduClass')->field("id,name,position_ids")->where($map)
            ->whereBetween("create_at",[ time()-12*30*24*3600, time() ])
            ->select()->toArray();
    }

    //查询6个月内所有正在接受报名的考核信息
    public function _get_exam_enroll(){
        $map = array("status"=>1);
        return $this->app->db->name('CmsEduExam')->field("id,name,position_ids")->where($map)
            ->whereBetween("create_at",[ time()-12*30*24*3600, time() ])
            ->select()->toArray();
    }

    //培训报名提交
    public function enrollresult(){

        if($this->request->isPost()){

            $data = $this->request->post();
            $data[ 'create_at'] = $data[ 'update_at'] = time();

            $position = $this->app->db->name('CmsEduWork')->alias("wp")
                ->field("wp.*,ew.name as work_name, ew.id as work_id")
                ->join('cms_edu_work ew', "ew.id=wp.parent_id")
                ->where(["wp.id"=>$data['work_position']])
                ->order('wp.id desc')->find();
            if($position==false){
                $this->error("注册失败，请稍候再试");
            }

            $enroll = array(
                "name" => $data['name'],
                "phone" => $data['phone'],
                "idno" => $data['idno'],
                "gender" => $data['gender'],
                "company" => $data['company'],
                "avatar" => $data['avatar'],
                "city" => $data['city'],
                "position_id" => $position['id'],
                "position_name" => $position['name'],
                "work_id" => $position['work_id'],
                "work_name" => $position['work_name'],
                "occur_time" => date("Y-m-d"),
                "class_id"=>$data['class_id'],
                "create_at" => time(),
                "update_at" => time(),
            );
            //重复报名判断
            $map = ["idno"=>$data['idno'], "class_id"=>$data['class_id']];
            if($this->app->db->name("CmsEduEnroll")->where($map)->find()){
                $this->success("您已提交报班申请，不能重复提交");
            }else{
                //执行报名逻辑
                $enroll['no'] = $this->_generate_enroll_no($data);
                $enroll['create_by'] = session("member.id"); //创建人
                $this->app->db->name("CmsEduEnroll")->insertGetId($enroll);
                $this->success("报名成功，报名号：".$enroll['no']);
            }
        }else{
            $this->error("不支持的请求方式");
        }
    }

    //考核报名提交
    public function examenrollresult(){

        if($this->request->isPost()){

            $data = $this->request->post();
            $data[ 'create_at'] = $data[ 'update_at'] = time();

            $position = $this->app->db->name('CmsEduWork')->alias("wp")
                ->field("wp.*,ew.name as work_name, ew.id as work_id")
                ->join('cms_edu_work ew', "ew.id=wp.parent_id")
                ->where(["wp.id"=>$data['work_position']])
                ->order('wp.id desc')->find();
            if($position==false){
                $this->error("注册失败，请稍候再试");
            }

            $enroll = array(
                "name" => $data['name'],
                "phone" => $data['phone'],
                "idno" => $data['idno'],
                "gender" => $data['gender'],
                "company" => $data['company'],
                "city" => $data['city'],
                "avatar" => $data['avatar'],
                "position_id" => $position['id'],
                "position_name" => $position['name'],
                "work_id" => $position['work_id'],
                "work_name" => $position['work_name'],
                "occur_time" => date("Y-m-d"),
                "exam_id"=>$data['exam_id'],
                "create_at" => time(),
                "update_at" => time(),
            );
            //重复报名判断
            $map = ["idno"=>$data['idno'], "exam_id"=>$data['exam_id']];
            if($this->app->db->name("CmsEduExamEnroll")->where($map)->find()){
                $this->success("您已提交报班申请，不能重复提交");
            }else{
                //执行报名逻辑
                $map = [ "id"=> $enroll['exam_id']];
                $exam = $this->app->db->name("CmsEduExam")->where($map)->find();
                $enroll['exam_name'] = $exam['name'];
                $enroll['create_by'] = session("member.id"); //创建人
                $enroll['no'] = $this->_generate_exam_enroll_no($data);
                $this->app->db->name("CmsEduExamEnroll")->insertGetId($enroll);
                $this->success("报名成功，报名号：".$enroll['no']);
            }
        }else{
            $this->error("不支持的请求方式");
        }
    }

    //导入培训报名信息
    public function enrollupload(){
        //$inputFileName = "F:\\phpStudy\\GasPortalProject\\temp\\01simple.xlsx";
        //$inputFileName = "http://127.0.0.1:9500/upload/63/1fcfb478ee9a562f929784a1b44e64.xlsx";
        $inputFileName = $this->request->request("upload-enroll");
        $inputFileName = $this->filerFilePath($inputFileName);

        //读取数据源
        $member = session("member");
        $fields = [
            "class_id","name","gender","city","company",
            "idno", "work_name","position_name","phone"
        ];
        $data = $this->getImportDataFromExcel($inputFileName, $fields);
        if($data==false){
            $this->error("没有数据需要被导入");
        }

        //有效性检查
        for ($i=0; $i<count($data); $i++) {
            if($data[$i]['name']==false || $data[$i]['idno']==false){
                //无效的数据
                continue;
            }

            if(strlen($data[$i]['idno'])!=18){
                $this->error("第".($i+2)."行数据有误,身份证:".$data[$i]['idno']."格式有误");
            }

            if(strlen($data[$i]['phone'])!=11){
                $this->error("第".($i+2)."行数据有误,手机号:".$data[$i]['phone']."格式有误");
            }

            $map = [ "idno"=>$data[$i]['idno'], "class_id"=> $data[$i]['class_id'] ];
            if($this->app->db->name("CmsEduEnroll")->where($map)->find()){
                $this->error("第".($i+2)."行信息有误，".$data[$i]['name']."(".$data[$i]['idno'].")已经报名该培训班，请不要重复报名");
            }

            $map = ["name"=>$data[$i]['class_id']];
            $class = $this->app->db->name("CmsEduClass")->field("id,name")->where($map)->find();
            if($class==null){
                $this->error("第".($i+2)."行信息有误，培训班名称：".$data[$i]['class_id']."无效");
            }else{
                $data[$i]['class_id'] = $class['id'];
            }

            $map = [ "wp.name"=>$data[$i]['work_name'], "ew.name"=>$data[$i]['position_name'] ];
            $work = $this->app->db->name('CmsEduWork')->alias("wp")
                ->field("wp.id as work_id, wp.name as work_name,ew.name as position_name, ew.id as position_id")
                ->join('cms_edu_work ew', "wp.id=ew.parent_id")
                ->where($map)
                ->order('wp.id desc')->find();
            if($work==false){
                $this->error("第".($i+2)."行信息有误，岗位:".$data[$i]['work_name']."及工种:".$data[$i]['position_name']."填写有误");
            }
            $data[$i]['work_id'] = $work['work_id'];
            $data[$i]['position_id'] = $work['position_id'];
            $data[$i]['occur_time'] = date("Y-m-d");
            $data[$i]['create_by'] = session("member.id");
        }

        //写入数据
        for ($i=0; $i<count($data); $i++) {
            $data[$i]['create_by'] = $member['id'];
            $data[$i]['create_at'] = time();
            $data[$i]['no'] = $this->_generate_enroll_no($data[$i]);
            $this->app->db->name($this->table)->save($data[$i]);
        }

        $this->success("导入成功");

    }

    //导入考核报名信息
    public function examenrollupload(){
        //$inputFileName = "F:\\phpStudy\\GasPortalProject\\temp\\01simple.xlsx";
        //$inputFileName = "http://127.0.0.1:9500/upload/63/1fcfb478ee9a562f929784a1b44e64.xlsx";
        $inputFileName = $this->request->request("upload-enroll");
        $inputFileName = $this->filerFilePath($inputFileName);

        //读取数据源
        $member = session("member");
        $fields = [
            "exam_name","name","gender","city","company",
            "idno", "work_name","position_name","phone"
        ];

        $data = $this->getImportDataFromExcel($inputFileName, $fields);
        if($data==false){
            $this->error("没有数据需要被导入");
        }

        //有效性检查
        for ($i=0; $i<count($data); $i++) {
            if($data[$i]['name']==false || $data[$i]['idno']==false){
                //无效的数据
                continue;
            }

            if(strlen($data[$i]['idno'])!=18){
                $this->error("第".($i+2)."行数据有误,身份证:".$data[$i]['idno']."格式有误");
            }

            if(strlen($data[$i]['phone'])!=11){
                $this->error("第".($i+2)."行数据有误,手机号:".$data[$i]['phone']."格式有误");
            }

            $map = ["name"=>$data[$i]['exam_name']];
            $class = $this->app->db->name("CmsEduExam")->field("id,name")->where($map)->find();
            if($class==null){
                $this->error("第".($i+2)."行数据有误，"."考核名称：".$data[$i]['class_id']."无效");
            }else{
                $data[$i]['exam_id'] = $class['id'];
            }

            $map = [ "idno"=>$data[$i]['idno'], "exam_id"=> $data[$i]['exam_id'] ];
            if($this->app->db->name("CmsEduExamEnroll")->where($map)->find()){
                $this->error("第".($i+2)."行数据有误，".$data[$i]['name']."(".$data[$i]['idno'].")已经报名，请不要重复报名");
            }

            $map = [ "wp.name"=>$data[$i]['work_name'], "ew.name"=>$data[$i]['position_name'] ];
            $work = $this->app->db->name('CmsEduWork')->alias("wp")
                ->field("wp.id as work_id, wp.name as work_name,ew.name as position_name, ew.id as position_id")
                ->join('cms_edu_work ew', "wp.id=ew.parent_id")
                ->where($map)
                ->order('wp.id desc')->find();
            if($work==false){
                $this->error("第".($i+2)."行数据有误，"."岗位:".$data[$i]['work_name']."及工种:".$data[$i]['position_name']."信息填写有误");
            }
            $data[$i]['work_id'] = $work['work_id'];
            $data[$i]['position_id'] = $work['position_id'];
        }

        //写入数据
        for ($i=0; $i<count($data); $i++) {
            $data[$i]['create_by'] = $member['id'];
            $data[$i]['create_at'] = time();
            $data[$i]['occur_time'] = date("Y-m-d");
            $data[$i]['no'] = $this->_generate_exam_enroll_no($data[$i]);
            $this->app->db->name("CmsEduExamEnroll")->save($data[$i]);
        }

        $this->success("导入成功");

    }

    public function ajaxSearch()
    {
        $limit = 5;
        $page = intval($this->request->request('page',1));
        $offset = ($page-1)*$limit;
        $member = session("member");
        $map = [ "ee.is_deleted"=> 0,"ee.create_by"=> $member['id'] ];
        $name = $this->request->request('name');
        if($name!=""){
            $category = $this->app->db->name('CmsEduEnroll')
                ->field("ee.*,ec.name as class_name,ec.status as class_status,ec.addr as class_addr,ec.start_at as class_start_at")
                ->join('cms_edu_class ec', "ec.id=ee.class_id")
                ->where($map)
                ->whereLike("ee.name","%$name%")
                ->order("ee.id desc")
                ->limit($offset,$limit)->select()->toArray();
            $total = $this->app->db->name('CmsEduEnroll')->alias("ee")
                ->where($map)
                ->whereLike("ee.name","%$name%")->count();
        }else{
            $category = $this->app->db->name('CmsEduEnroll')->alias("ee")
                ->field("ee.*,ec.name as class_name,ec.status as class_status,ec.addr as class_addr,ec.start_at as class_start_at")
                ->join('cms_edu_class ec', "ec.id=ee.class_id")
                ->where($map)
                ->order("ee.id desc")
                ->limit($offset,$limit)->select()->toArray();
            $total = $this->app->db->name('CmsEduEnroll')->alias("ee")->where($map)->count();
        }
        $this->success("成功",array("list"=>$category,"total"=>$total,"limit"=>$limit));
    }

    public function ajaxSearchCert()
    {
        $limit = 5;
        $page = intval($this->request->request('page',1));
        $offset = ($page-1)*$limit;
        $member = session("member");
        $map = [ "ec.create_by"=> $member['id'] ];
        $category = $this->app->db->name('CmsEduCert')->alias("ec")
            ->where($map)
            ->order("ec.id desc")
            ->limit($offset,$limit)->select()->toArray();
        $total = $this->app->db->name('CmsEduCert')->alias("ec")->where($map)->count();
        $this->success("成功",array("list"=>$category,"total"=>$total,"limit"=>$limit));
    }

    public function ajaxSearchExam()
    {

        $limit = 5;
        $page = intval($this->request->request('page',1));
        $offset = ($page-1)*$limit;
        $member = session("member");
        $map = [ 'ee.is_deleted'=>0, "ee.create_by"=> $member['id'] ];
        $category = $this->app->db->name('CmsEduExamEnroll')->alias("ee")
            ->field("ee.*,ea.name as exam_name")
            ->join('cms_edu_exam ea', "ea.id=ee.exam_id")
            ->where($map)
            ->order("ee.id desc")
            ->limit($offset,$limit)->select()->toArray();
        $total = $this->app->db->name('CmsEduExamEnroll')->alias("ee")->where($map)->count();

        $this->success("成功",array("list"=>$category,"total"=>$total,"limit"=>$limit));
    }
}